package com.example.demo.service;

import com.example.demo.dao.UserDAO;
import com.example.demo.entity.Address;
import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

    @Autowired
    private UserDAO userDAO;



    public void insertnewUser(String userName, String password, String phone)
    {

        userDAO.insertnewUser(userName,password,phone);
    }

    public User selectUserByPhoneAndPassword(String phone,String password)
    {
        User user;
        user=userDAO.selectUserByPhoneAndPassword(phone,password);
        return user;
    }
    public User findByToken(String token)
    {
        return userDAO.findByToken(token);
    }


    public User findByPhone(String phone)
    {
        return userDAO.findByPhone(phone);
    }

    public User findById(int id)
    {
        return userDAO.findById(id);
    }

    public void updateById(User user)
    {
        userDAO.updateById(user.getUserid(),user.getUsername(),user.getPassword());
    }

    public void updateVIP(User user)
    {
        userDAO.updateVIP(user.getUserid(),user.getIdCard(),user.getRealname());
    }

    public List<Address> getAddress(int userid)
    {
        return userDAO.getAddress(userid);
    }

    public void insertnewAddress(Address address)
    {
        userDAO.insertnewAddress(address);
    }

    public void updateAddress(String address, String phone, String name,int addressid,String region)
    {
        userDAO.updateAddress(address,phone,name,addressid,region);
    }

    public void deleteAddress(int addressid)
    {
        userDAO.deleteAddress(addressid);
    }

    public Address findAddressById(int addressid)
    {
        return userDAO.findAddressById(addressid);
    }

    public void bindUser(int userid,String token)
    {
        userDAO.bindUser(userid,token);
    }

    public void addMoney(int userid,double point)
    {
        userDAO.addMoney(userid,point);
    }

    public List<User> getUserList(int currentPage,String searchMsg){
        int star = (currentPage-1)*10;
        List<User> users = userDAO.getUserList(star,searchMsg);
        return users;
    }

    public int getUserListTotal(String searchMsg){
        return userDAO.getUserListTotal(searchMsg);
    }

    public void deleteUserById(int id){
        userDAO.deleteUserById(id);
    }

    public void editUser(User user){
        userDAO.editUser(user);
    }

}
